/*
 * Project: BooEditor
 * Author:  Johannes Kuhaupt
 * Last Change:
 *    by:   $Author: $
 *    date: $Date:   $
 * Copyright (c): Johannes Kuhaupt
 * Licence: Creative Commons CC BY-NC-SA 3.0
 */
package control.boo;

import control.GuiManager;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Johannes Kuhaupt
 */
public class AbstractJavaTask extends Thread{
    
    String compilerResult="";
    Process currentProcess;
    ProcessBuilder pb;
    File taskFile;
    boolean processStarted;
    GuiManager guiMan = GuiManager.getInstance();
    
    protected void readOutputStream(){
        BufferedReader b = new BufferedReader(new InputStreamReader(this.currentProcess.getInputStream()));
        int lineCount = 0;
        String result = "";
        String tmp="";
        
        try {
            while ((tmp = b.readLine()) != null) {
                lineCount++;
                result = result + tmp + "\n";
                this.compilerResult = result;

            }
            
            
            
    
        } catch (IOException ex) {
            this.guiMan.showErrorMessage(GuiManager.getLangBundle().getString("MsgErrorCompiling"));
            Logger.getLogger(AbstractJavaTask.class.getName()).log(Level.SEVERE, null, ex);
            this.guiMan.setOutputInFrame(this.compilerResult);
            System.err.println("Error, readOutputStream failed: " + ex.toString());
        }
    }
    
    /**
     *
     * @return
     */
    public String getCompilerResult(){
        return this.compilerResult;
    }
    
    /**
     *
     */
    public void clearResult(){
        this.compilerResult = "";
    }
    
    /**
     *
     * @param pFile
     * @return
     */
    protected String getFileWithoutEnding(File pFile){
       String fileName = pFile.getName();
       String out  = fileName.substring(0, fileName.lastIndexOf('.'));
       return out;
   }
    
    
    
    
    /**
     *
     * @param f
     */
    public void setTaskFile(File f){
        taskFile = f;
    }
    
    
    
    /**
     *
     */
    public void stopLejosTask(){
         currentProcess.destroy();
    }
    
}
